import configdata from '@/common/config'

module.exports = {
	dealHeader: function(header) {
		var reqParams = {}
		reqParams['content-type'] = header;
		const isLogin = uni.getStorageSync("isLogin");
		if (isLogin) {
			const accessToken = uni.getStorageSync("accessToken");
			const userId = uni.getStorageSync("userId");
			const expireTime = uni.getStorageSync("expireTime");
			reqParams['YF-TOKEN'] = accessToken;
			reqParams['userId'] = userId;
			reqParams['expireTime'] = expireTime;
		}
		console.log(reqParams);
		return reqParams;
	},
	dealResult: function(result) {
		if (result != '' && result.code == "10002") {
			uni.removeStorageSync("userInfo");
			uni.removeStorageSync("accessToken");
			uni.removeStorageSync("userId");
			uni.removeStorageSync("expireTime");
			uni.setStorageSync("isLogin", false);
			uni.reLaunch({
			    url: '/pages/login/wxlogin'
			})
		}
		return result;
	},
	postJson: function (url, data, header) {
	    header = header || "application/json;charset=UTF-8";
	    url = configdata.baseUrl + url;
		var that = this;
		console.log("*********************入参");
		console.log(data);
	    return new Promise((succ, error) => {
	        uni.request({
	            url: url,
	            data: data,
	            method: "POST",
	            header: this.dealHeader(header),
	            success: function (result) {
	                console.log(result);
	                succ.call(self, that.dealResult(result.data))
	            },
	            fail: function (e) {
	                error.call(self, e)
	            }
	        })
	    })
	},
	postParamsLoadView:function(url,data,header){
		url = configdata.baseUrl + url;
		header = header || "application/x-www-form-urlencoded";
		uni.showLoading({
			title: "加载中...",
			mask: true
		})
		var that = this;
		console.log("*********************入参");
		console.log(data);
		return new Promise((succ, error) => {
		    uni.request({
		        url: url,
		        data: data,
		        method: "POST",
		        header: this.dealHeader(header),
		        success: function (result) {
		            console.log(result);
					uni.hideLoading()
		            succ.call(self, that.dealResult(result.data))
		        },
		        fail: function (e) {
					uni.hideLoading()
		            error.call(self, e)
		        }
		    })
		})
	},
	postJsonLoadView: function (url, data, header) {
	    header = header || "application/json;charset=UTF-8";
	    url = configdata.baseUrl + url;
		uni.showLoading({
			title: "加载中...",
			mask: true
		})
		var that = this;
		console.log("*********************入参");
		console.log(data);
	    return new Promise((succ, error) => {
	        uni.request({
	            url: url,
	            data: data,
	            method: "POST",
	            header: this.dealHeader(header),
	            success: function (result) {
	                console.log(result);
					uni.hideLoading()
	                succ.call(self, that.dealResult(result.data))
	            },
	            fail: function (e) {
					uni.hideLoading()
	                error.call(self, e)
	            }
	        })
	    })
	}
}
